// 
class Storage {
    constructor() {
        if(!Storage.instance) {
            Storage.instance = this
        }
        return Storage.instance
    }

    setItem(name, val) {
        return localStorage.setItem(name, val)
    }

    getItem(name) {
        return localStorage.getItem(name)
    } 
}

let storage1 = new Storage()
let storage2 = new Storage()
console.log(storage1 === storage2)
// 

function StorageBase() {}
StorageBase.prototype.getItem = function (key) {
    return localStorage.getItem(key)
}
StorageBase.prototype.setItem = function (key, val) {
    return localStorage.setItem(key, val)
}

const es5Storage = (function () {
    let instance = null
    return function () {
        if (!instance) {
            instance = new StorageBase()
        }
        return instance
    }
}())

console.log(new es5Storage() === new es5Storage())




// 使用单例模式实现全局模态框
class Modal {
    constructor () {
        this.dom = null
        if(!Modal.instance) {
            this.dom = document.createElement("div")
            this.dom.innerHTML = "我是modal"
            this.dom.style.display = "none"
            document.body.appendChild(this.dom)
            this.dom.showModal = this.show.bind(this)
            this.dom.hideModal = this.hide.bind(this)
            Modal.instance = this.dom
        }
        return Modal.instance
    }

    show() {
        this.dom.style.display = "block"
    }

    hide() {
        this.dom.style.display = "none"
    }
}


var es5Modal = (function () {
    var instance = null
    return function (title, content) {
        if (!instance) {
            this.title = title
            this.content = content
            this.update = function (key, val) {
                const is = Object.keys(this).some(item => item === key)
                if (is) {
                    this[key] = val
                    return this
                } else {
                    console.error("The key to be modified does not exist")
                }
            }
            instance = this
        }
        return instance
    }
})()

var es5m1 = new es5Modal("温馨提示", "你确定要前进吗？")
var es5m2 = es5m1.update("content", "321456456")
var es5m3 = new es5Modal()